package cn.tedu.coolshark.mapper;

import cn.tedu.coolshark.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Classname: ProductMapper
 * @Author: bromide
 * @CreateTime: 2022/5/18--11:49
 * @Version: V1.0
 * @Description:
 */
@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{url},#{price},#{oldPrice},#{viewCount},#{saleCount},#{created},#{categoryId})")
    void insert(Product product);

    @Select("select id,title,price,sale_count,url from product")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> select();

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);

    @Select("select id,title,url,price,old_price,sale_count from product")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectIndex();

    @Select("select id,title,sale_count from product order by sale_count desc limit 0,6")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectTop();

    @Select("select id,title,url,price,old_price,sale_count from product where category_id=#{cid}")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectResult(int cid);

    //                                                        concat是SQL语句中将多个字符串进行拼接的函数
    @Select("select id,title,url,price,old_price,sale_count from product where title like concat('%',#{wd},'%')")
    @Result(property = "oldPrice",column = "old_price")
    @Result(property = "saleCount",column = "sale_count")
    List<Product> selectByWd(String wd);

    @Select("select id,title,url,price,old_price,sale_count,view_count,created from product where id=#{id}")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "view_count",property = "viewCount")
    Product selectById(int id);

    @Update("update product set view_count=view_count+1 where id=#{id}")
    void updateViewCount(int id);

    @Delete("delete from product where category_id=#{id}")
    void deleteByCid(int id);


    @Select("select url from product where category_id=#{id}")
    List<String> selectUrlByCid(int id);
}
